package com.jjm2473.dict;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collection;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author liangbin.llb
 * @since 2020/4/1
 */
public class Main {
    public static void main(String[] args) {
        Set<String> words = Stream.of(args).map(Main::readDictSafe)
                .flatMap(Collection::stream)
                .collect(Collectors.toSet());
        Dictionary dict = new DictionaryImpl(words);
        Tokenizer tokenizer = new Tokenizer(dict);
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("Input:");
            if (input.hasNextLine()) {
                String s = input.nextLine();
                System.out.println("Output:");
                System.out.println(String.join("\n", tokenizer.tokenize(s)));
            } else {
                break;
            }
        }
    }

    private static Set<String> readDictSafe(String fileName) {
        try {
            return readDict(fileName);
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException(fileName + " not found", e);
        }
    }

    private static Set<String> readDict(String fileName) throws FileNotFoundException {
        Scanner scanner = new Scanner(new File(fileName));
        Set<String> words = new HashSet<>();
        while (scanner.hasNextLine()) {
            words.add(scanner.nextLine());
        }
        return words;
    }
}
